package com.example.gw_back_end.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.entities.GameCategory;
import com.example.gw_back_end.service.GameCategoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("game_category")
public class GameCategoryController {
    private final GameCategoryService gameCategoryService;
    public GameCategoryController(GameCategoryService gameCategoryService) {
        this.gameCategoryService = gameCategoryService;
    }

    @GetMapping("get_game_category")
    public Result getGameCategory(Integer id) {
        LambdaQueryWrapper<GameCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GameCategory::getGameId, id);
        return Result.success(gameCategoryService.list(lambdaQueryWrapper));
    }

    @PostMapping("add_game_category")
    public Result addGameCategory(Integer gameId, List<Integer> categoryIdList) {
        List<GameCategory> gameCategories = new ArrayList<>();
        categoryIdList.forEach((item) -> {
            GameCategory gameCategory = new GameCategory();
            gameCategory.setGameId(gameId);
            gameCategory.setCategoryId(item);
            gameCategories.add(gameCategory);
        });
        if (gameCategoryService.saveBatch(gameCategories)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }
}
